package org.seven.jrdp.biz.sys.service;

import java.util.List;
import java.util.Map;

import org.seven.jrdp.biz.sys.mapper.SysUserMapper;
import org.seven.jrdp.biz.sys.mapper.SysUserRoleMapper;
import org.seven.jrdp.biz.sys.model.SysUser;
import org.seven.jrdp.biz.sys.model.SysUserRole;
import org.seven.jrdp.commons.exception.BizException;
import org.seven.jrdp.commons.model.Option;
import org.seven.jrdp.commons.mybatis.model.PageRequest;
import org.seven.jrdp.commons.util.CipherUtils;
import org.seven.jrdp.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@CacheConfig(cacheNames = "user")
@Service
public class SysUserService {
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;

	@Transactional(readOnly = true)
	public PageInfo<SysUser> find(Map<String, Object> map, PageRequest pageRequest) {
		PageHelper.startPage(pageRequest.getPage(), pageRequest.getRows());
		return new PageInfo<SysUser>(sysUserMapper.find(map));
	}

	@Transactional(readOnly = true)
	public SysUser get(String id) {
		return sysUserMapper.get(id);
	}

	@CacheEvict(cacheNames = { "organ", "role", "user", "mgt" }, allEntries = true)
	@Transactional
	public void insert(SysUser sysUser) {
		Example example = new Example(SysUser.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("account", sysUser.getAccount());
		if (sysUserMapper.selectCountByExample(example) > 0) {
			throw new BizException("帐号不允许重复");
		}
		sysUser.setId(StringUtils.uuid());
		sysUser.setPassword(CipherUtils.md5(sysUser.getAccount(), sysUser.getId()));
		sysUserMapper.insertSelective(sysUser);
		saveRole(sysUser.getId(), sysUser.getRole());
	}

	@CacheEvict(cacheNames = { "organ", "role", "user", "mgt" }, allEntries = true)
	@Transactional
	public void update(SysUser sysUser) {
		Example example = new Example(SysUser.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("account", sysUser.getAccount());
		criteria.andNotEqualTo("id", sysUser.getId());
		if (sysUserMapper.selectCountByExample(example) > 0) {
			throw new BizException("帐号不允许重复");
		}
		sysUserMapper.updateByPrimaryKeySelective(sysUser);
		saveRole(sysUser.getId(), sysUser.getRole());
	}

	@CacheEvict(cacheNames = { "organ", "role", "user", "mgt" }, allEntries = true)
	@Transactional
	public void delete(String id) {
		SysUser sysUser = new SysUser();
		sysUser.setId(id);
		sysUser.setDeleteFlag("1");
		sysUserMapper.updateByPrimaryKeySelective(sysUser);
	}

	@CacheEvict(cacheNames = "mgt", allEntries = true)
	@Transactional
	public void resetPassword(String id) {
		SysUser record = sysUserMapper.selectByPrimaryKey(id);
		SysUser sysUser = new SysUser();
		sysUser.setId(id);
		sysUser.setPassword(CipherUtils.md5(record.getAccount(), record.getId()));
		sysUserMapper.updateByPrimaryKeySelective(sysUser);
	}

	@Transactional(readOnly = true)
	public SysUser getByAccount(String account) {
		SysUser sysUser = new SysUser();
		sysUser.setAccount(account);
		sysUser.setDeleteFlag("0");
		sysUser.setLocked("0");
		return sysUserMapper.selectOne(sysUser);
	}

	@Cacheable(key = "'user_option_'+#organCode+'_'+#roleCode")
	@Transactional(readOnly = true)
	public List<Option> findOption(String organCode, String roleCode) {
		return sysUserMapper.findOption(organCode, roleCode);
	}

	@Cacheable(key = "'user_all'")
	@Transactional(readOnly = true)
	public List<Option> findAll() {
		return sysUserMapper.findAll();
	}

	@Cacheable(key = "'user_map'")
	@Transactional(readOnly = true)
	public List<Map<String, Object>> findMap() {
		return sysUserMapper.findMap();
	}

	private void saveRole(String id, String[] roleIds) {
		{
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setUserId(id);
			sysUserRoleMapper.delete(sysUserRole);
		}
		if (roleIds != null) {
			for (String roleId : roleIds) {
				SysUserRole sysUserRole = new SysUserRole();
				sysUserRole.setUserId(id);
				sysUserRole.setRoleId(roleId);
				sysUserRoleMapper.insertSelective(sysUserRole);
			}
		}
	}
	
	@Transactional(readOnly = true)
	public String getName(String id) {
		return sysUserMapper.getName(id);
	}
}